package com.muniuliuma.basecontrol;



import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.Display;

public class MenuControl {
	private static boolean sLocked = false;
	public static Activity sCurrentActivity = null;

	
	public static boolean isWinscapeLocked() {
		return sLocked;
	}

	/**
	 * lock current screen
	 * */
	public static void lockWinscape(boolean bLock) {
		sLocked = bLock;
	}
	
	/**
	 * get Previous activity;
	 * */
	public static Activity getPreviousActivity(){
		return ActivityStack.getInstance().getPreviousActivity();
	}
	/**
	 * get orientation states
	 * 
	 * @return true if screen orientation is portrait, false otherwise.
	 * 
	 * */
	public static boolean isPortrait(){
		int orientation = sCurrentActivity.getResources().getConfiguration().orientation;
		return orientation==Configuration.ORIENTATION_PORTRAIT;
	}
	
	/**
	 * get size of screen
	 * 
	 * @return size of index 0 is width, size of index 1 is height
	 * */
	public static int[] sizeOfScreen(){
		Display display = sCurrentActivity.getWindowManager().getDefaultDisplay();
		
		return new int[]{display.getWidth(), display.getHeight()};
	}
	
	/**get width of screen*/
	public static int getWidthOfScreen(){
		return sizeOfScreen()[0];
	}
	
	/**get height of screen*/
	public static int getHeightOfScreen(){
		return sizeOfScreen()[1];
	}
	
	/**
	 * start a new activity
	 * */
	public static void changeWinscape(Activity thiz, Class<? extends Activity> cls) {
		changeWinscape(thiz, cls, false);
	}

	/**
	 * start a new activity, but you can control if store current activity
	 * 
	 * @param needFinish true if you want to finish current activity
	 * */
	public static void changeWinscape(Activity thiz,
			Class<? extends Activity> cls, boolean needFinish) {
		lockWinscape(true);
		Intent intent = new Intent();
		intent.setClass(thiz, cls);
		thiz.startActivity(intent);
		
		if(needFinish){
			thiz.finish();
		}
	}

	/**
	 * back to previous activity
	 * 
	 * @param thiz current activity itself
	 * */
	public static void backward(Activity thiz) {
		thiz.finish();
	}

	/**
	 * 
	 * Back to the Activity, if that activity is not exist in the stack, start a new activity
	 * 
	 * */ 
	public static void backward(Activity thiz, Class<? extends Activity> cls) {

		if (ActivityStack.getInstance().hasActivity(cls)) {
			lockWinscape(true);
			while (!ActivityStack.getInstance().topActivity().getClass().equals(cls)) {
				ActivityStack.getInstance().topActivity().finish();
				ActivityStack.getInstance().popActivity();
			}
		} else {
			changeWinscape(thiz, cls);
		}
	}


}
